<?php

/**
 * kol红人管理
 * (model)
 */
namespace App\Models\KolManage;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysCountry;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use Illuminate\Database\Eloquent\Model;

class Kol extends Model{

    protected $table = 'k_kol';

    protected $guarded  = [];

    /**
     * 状态
     */
    const STATUS_YES  = 1; //启用
    const STATUS_NO   = 2; //停用
    public static $_status = [
        self::STATUS_YES  => '启用',
        self::STATUS_NO   => '停用',
    ];

    /**
     * 社媒平台
     */
    const PLATFORM_0  = 0; //无
    const PLATFORM_1  = 1; //YouTube
    const PLATFORM_2  = 2; //Facebook
    const PLATFORM_3  = 3; //TikTok
    const PLATFORM_4  = 4; //Instagram
    public static $_media_platform = [
        self::PLATFORM_0 => '无',
        self::PLATFORM_1 => 'YouTube',
        self::PLATFORM_2 => 'Facebook',
        self::PLATFORM_3 => 'TikTok',
        self::PLATFORM_4 => 'Instagram'
    ];

    /**
     * 社媒分区
     */
    const ZONE_0  = 0; //无
    const ZONE_1  = 1; //垂直类
    const ZONE_2  = 2; //测评类
    const ZONE_3  = 3; //户外类
    public static $_media_zone = [
        self::ZONE_0 => '无',
        self::ZONE_1 => '垂直类',
        self::ZONE_2 => '测评类',
        self::ZONE_3 => '户外类',
    ];

    /**
     * 社媒粉丝体量
     */
    const FOLLOWER_0  = 0; //无
    const FOLLOWER_1  = 1; //纳米级（1000~1w）
    const FOLLOWER_2  = 2; //超小型（1w~5w）
    const FOLLOWER_3  = 3; //小型（5w~50w）
    const FOLLOWER_4  = 4; //中等（50w~100w）
    const FOLLOWER_5  = 5; //大型（100w+）
    public static $_media_follower = [
        self::FOLLOWER_0 => '无',
        self::FOLLOWER_1 => '纳米级（1000~1w）',
        self::FOLLOWER_2 => '超小型（1w~5w）',
        self::FOLLOWER_3 => '小型（5w~50w）',
        self::FOLLOWER_4 => '中等（50w~100w）',
        self::FOLLOWER_5 => '大型（100w+）',
    ];

    /**
     * 风格类型
     */
    const TYPE_0  = 0; //无
    const TYPE_1  = 1; //露营类
    const TYPE_2  = 2; //电子类
    const TYPE_3  = 3; //冬季产品
    public static $_media_style = [
        self::TYPE_0 => '无',
        self::TYPE_1 => '露营类',
        self::TYPE_2 => '电子类',
        self::TYPE_3 => '冬季产品',
    ];

    /**
     * 合作评级
     */
    const GRADE_0  = 0; //无
    const GRADE_1  = 1; //一级
    const GRADE_2  = 2; //二级
    const GRADE_3  = 3; //三级
    const GRADE_4  = 4; //四级
    const GRADE_5  = 5; //五级
    public static $_cooperate_grade = [
        self::GRADE_0 => '无',
        self::GRADE_1 => '一级',
        self::GRADE_2 => '二级',
        self::GRADE_3 => '三级',
        self::GRADE_4 => '四级',
        self::GRADE_5 => '五级',
    ];

    public static function options($where = []){
        $query = $where ? self::query()->where([$where]) : self::query();
        return $query->pluck('kol_code','id');
    }

    public function sku(){
        return $this->hasMany(KolSku::class, 'kol_code', 'kol_code');
    }

    public function area(){
        return $this->belongsTo(SysArea::class, 'area_id');
    }

    public function country(){
        return $this->belongsTo(SysCountry::class, 'country_id');
    }

    public function skuClassify(){
        return $this->hasOneThrough(
            ProductClassify::class,
            ProductSku::class,
            'id',//中间表
            'id',//目标表
            'psku_id',//当前模型
            'classify_id'//中间表
        );
    }

    public static function getDownloadData($res)
    {
        foreach ($res as $model){
            $temp = [
                $model->name ?? '',
                $model->email ?? '',
                $model->mobile ?? '',
                $model->address ?? '',
                $model->area->area ?? '',
                $model->country->country ?? '',
                $model->media_account ?? '',
                self::$_media_platform[$model->media_platform] ?? '',
                self::$_media_zone[$model->media_zone] ?? '',
                self::$_media_follower[$model->media_follower] ?? '',
                self::$_media_style[$model->media_style] ?? '',
                self::$_cooperate_grade[$model->cooperate_grade] ?? '',
                $model->cooperate_time ?? '',
                $model->remark ?? '',
                self::$_status[$model->status] ?? '',
            ];
            yield $temp;
        }
    }

    public static function getModelById($id){
        return self::query()->find($id);
    }

    public static function getModelId($kolCode){
        return self::query()->where('kol_code',$kolCode)->value('id');
    }

    public static function getValueById($id,$field){
        return self::query()->where('id',$id)->value($field);
    }
}
